/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCadastroCursos.java
 *
 * Created on 21/09/2009, 17:56:23
 */

package cursos.telas;

import cursos.bd.CursosDAO;
import cursos.classes.Cursos;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import professor.classes.Professor;
import professor.telas.FrmConsultaProfessor;
import utilitarios.ItelaConsulta;

/**
 *
 * @author Gabi
 */
public class FrmCadastroCursos extends javax.swing.JFrame implements ItelaConsulta {
    /** Creates new form FrmCadastroCursos */
    public FrmCadastroCursos(Cursos curso) {
        initComponents();
        setLocationRelativeTo(null);
        setVisible(true);
        try {
            cursosDAO = new CursosDAO();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            ex.printStackTrace();
        }
        this.curso = curso;
        this.professor = curso.getProfessor();
        if (professor != null && curso != null){
            populaTela();
        }
        }
    private void populaTela() {
        nomeProfessorField.setText(curso.getProfessor().getNome());
        nomeCursojTextField.setText(curso.getNome());
        dataInicialJChooser.setDate(curso.getPeriodoInicial());
        dataFinalJChooser.setDate(curso.getPeriodoFinal());
        horaInicialFormattedText.setValue(curso.getHoraInicial());
        horaFinalFormattedText.setValue(curso.getHoraFinal());
        descricaoCursoTextArea.setText(curso.getDescricaoAtividade());
        segundaCheckBox.setSelected(verificaData(0));
        tercaCheckBox.setSelected(verificaData(1));
        quartaCheckBox.setSelected(verificaData(2));
        quintaCheckBox.setSelected(verificaData(3));
        sextaCheckBox.setSelected(verificaData(4));
        sabadoCheckBox.setSelected(verificaData(5));
    }
    private boolean verificaData(int indice){
        String dias = curso.getDiaCurso();
        if (dias == null){
            return false;
        }
        char dia = dias.charAt(indice);
        if (dia == '1'){
                return true;
        }
            return false;
    }
            /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cursosPainelFundo = new javax.swing.JPanel();
        nomeProfessorLabel = new javax.swing.JLabel();
        nomeProfessorField = new javax.swing.JTextField();
        dataInicialLabel = new javax.swing.JLabel();
        dataFinalLabel = new javax.swing.JLabel();
        descricaoCursoLabel = new javax.swing.JLabel();
        dataInicialJChooser = new com.toedter.calendar.JDateChooser();
        dataFinalJChooser = new com.toedter.calendar.JDateChooser();
        descricaoCursoScroll = new javax.swing.JScrollPane();
        descricaoCursoTextArea = new javax.swing.JTextArea();
        salvarFecharPainel = new javax.swing.JPanel();
        salvarButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();
        horaInicialFormattedText = new javax.swing.JFormattedTextField();
        horaFinalFormattedText = new javax.swing.JFormattedTextField();
        horaLabel = new javax.swing.JLabel();
        asLabel = new javax.swing.JLabel();
        diaSemanaPainel = new javax.swing.JPanel();
        segundaCheckBox = new javax.swing.JCheckBox();
        tercaCheckBox = new javax.swing.JCheckBox();
        quartaCheckBox = new javax.swing.JCheckBox();
        quintaCheckBox = new javax.swing.JCheckBox();
        sextaCheckBox = new javax.swing.JCheckBox();
        sabadoCheckBox = new javax.swing.JCheckBox();
        nomeCursojTextField = new javax.swing.JTextField();
        nomeCursoLabel = new javax.swing.JLabel();
        pesquisaToolbar = new javax.swing.JToolBar();
        pesquisarProfessorjButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Cursos");
        setLocationByPlatform(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        cursosPainelFundo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cursosPainelFundo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nomeProfessorLabel.setText("Nome do Professor");
        cursosPainelFundo.add(nomeProfessorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        nomeProfessorField.setEditable(false);
        cursosPainelFundo.add(nomeProfessorField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 37, 210, -1));

        dataInicialLabel.setText("Data Inicial");
        cursosPainelFundo.add(dataInicialLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        dataFinalLabel.setText("Data Final");
        cursosPainelFundo.add(dataFinalLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 120, -1, -1));

        descricaoCursoLabel.setText("Descrição");
        cursosPainelFundo.add(descricaoCursoLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 163, -1, -1));
        cursosPainelFundo.add(dataInicialJChooser, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 137, 100, -1));
        cursosPainelFundo.add(dataFinalJChooser, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 137, 100, -1));

        descricaoCursoTextArea.setColumns(20);
        descricaoCursoTextArea.setRows(5);
        descricaoCursoTextArea.setAlignmentY(0.0F);
        descricaoCursoScroll.setViewportView(descricaoCursoTextArea);

        cursosPainelFundo.add(descricaoCursoScroll, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 390, -1));

        salvarFecharPainel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        salvarFecharPainel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        salvarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/tick.png"))); // NOI18N
        salvarButton.setText("Salvar");
        salvarButton.setToolTipText("Salvar");
        salvarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarButtonActionPerformed(evt);
            }
        });
        salvarFecharPainel.add(salvarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 8, -1, -1));

        fecharButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/cross.png"))); // NOI18N
        fecharButton.setText("Fechar");
        fecharButton.setToolTipText("Fechar");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FecharTela(evt);
            }
        });
        salvarFecharPainel.add(fecharButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 7, -1, -1));

        cursosPainelFundo.add(salvarFecharPainel, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 290, 250, 40));

        try {
            horaInicialFormattedText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cursosPainelFundo.add(horaInicialFormattedText, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 37, 40, -1));

        try {
            horaFinalFormattedText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cursosPainelFundo.add(horaFinalFormattedText, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 37, 40, -1));

        horaLabel.setText("Horario");
        cursosPainelFundo.add(horaLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, -1, -1));

        asLabel.setText("ás");
        cursosPainelFundo.add(asLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 40, -1, -1));

        diaSemanaPainel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        diaSemanaPainel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        segundaCheckBox.setText("Segunda");
        diaSemanaPainel.add(segundaCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        tercaCheckBox.setText("Terça");
        diaSemanaPainel.add(tercaCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, -1, -1));

        quartaCheckBox.setText("Quarta");
        diaSemanaPainel.add(quartaCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        quintaCheckBox.setText("Quinta");
        diaSemanaPainel.add(quintaCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, -1, -1));

        sextaCheckBox.setText("Sexta");
        diaSemanaPainel.add(sextaCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        sabadoCheckBox.setText("Sabado");
        diaSemanaPainel.add(sabadoCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 50, -1, -1));

        cursosPainelFundo.add(diaSemanaPainel, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 70, 170, 80));
        cursosPainelFundo.add(nomeCursojTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 87, 210, -1));

        nomeCursoLabel.setText("Nome do Curso");
        cursosPainelFundo.add(nomeCursoLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        pesquisaToolbar.setFloatable(false);
        pesquisaToolbar.setRollover(true);

        pesquisarProfessorjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/magnifier.png"))); // NOI18N
        pesquisarProfessorjButton.setFocusable(false);
        pesquisarProfessorjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        pesquisarProfessorjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        pesquisarProfessorjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarProfessorjButtonActionPerformed(evt);
            }
        });
        pesquisaToolbar.add(pesquisarProfessorjButton);

        cursosPainelFundo.add(pesquisaToolbar, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 37, 40, 20));

        getContentPane().add(cursosPainelFundo, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 430, 350));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void FecharTela(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FecharTela
        this.dispose();
}//GEN-LAST:event_FecharTela
    private void pesquisarProfessorjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarProfessorjButtonActionPerformed
        new FrmConsultaProfessor(this);
    }//GEN-LAST:event_pesquisarProfessorjButtonActionPerformed
    private void salvarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarButtonActionPerformed
        this.curso.setDescricaoAtividade(descricaoCursoTextArea.getText());
        this.curso.setNome(nomeCursojTextField.getText());
        this.curso.setPeriodoFinal(dataFinalJChooser.getDate());
        this.curso.setPeriodoInicial(dataInicialJChooser.getDate());
        this.curso.setHoraFinal(horaFinalFormattedText.getText());
        this.curso.setHoraInicial(horaInicialFormattedText.getText());
        this.curso.setDiaCurso(getDiaCursos());
        this.curso.setProfessor(professor);
        try{
        cursosDAO.salvar(curso);
        JOptionPane.showMessageDialog(this,"Dados salvos com sucesso");
        }catch (Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,"Dados não foram salvos");
        }
    }//GEN-LAST:event_salvarButtonActionPerformed
    private String getDiaCursos(){
        String diasdasemana;
        if (segundaCheckBox.isSelected()){
            diasdasemana = "1";
        }else
            diasdasemana = "0";
        if (tercaCheckBox.isSelected()){
            diasdasemana = diasdasemana.concat("1");
        }else
            diasdasemana = diasdasemana.concat("0");
        if (quartaCheckBox.isSelected()){
            diasdasemana = diasdasemana.concat("1");
        }else
            diasdasemana = diasdasemana.concat("0");
        if (quintaCheckBox.isSelected()){
            diasdasemana = diasdasemana.concat("1");
        }else
            diasdasemana = diasdasemana.concat("0");
        if (sextaCheckBox.isSelected()){
            diasdasemana = diasdasemana.concat("1");
        }else
            diasdasemana = diasdasemana.concat("0");
        if (sabadoCheckBox.isSelected()){
            diasdasemana = diasdasemana.concat("1");
        }else
            diasdasemana = diasdasemana.concat("0");
        return diasdasemana;
    }
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel asLabel;
    private javax.swing.JPanel cursosPainelFundo;
    private com.toedter.calendar.JDateChooser dataFinalJChooser;
    private javax.swing.JLabel dataFinalLabel;
    private com.toedter.calendar.JDateChooser dataInicialJChooser;
    private javax.swing.JLabel dataInicialLabel;
    private javax.swing.JLabel descricaoCursoLabel;
    private javax.swing.JScrollPane descricaoCursoScroll;
    private javax.swing.JTextArea descricaoCursoTextArea;
    private javax.swing.JPanel diaSemanaPainel;
    private javax.swing.JButton fecharButton;
    private javax.swing.JFormattedTextField horaFinalFormattedText;
    private javax.swing.JFormattedTextField horaInicialFormattedText;
    private javax.swing.JLabel horaLabel;
    private javax.swing.JLabel nomeCursoLabel;
    private javax.swing.JTextField nomeCursojTextField;
    private javax.swing.JTextField nomeProfessorField;
    private javax.swing.JLabel nomeProfessorLabel;
    private javax.swing.JToolBar pesquisaToolbar;
    private javax.swing.JButton pesquisarProfessorjButton;
    private javax.swing.JCheckBox quartaCheckBox;
    private javax.swing.JCheckBox quintaCheckBox;
    private javax.swing.JCheckBox sabadoCheckBox;
    private javax.swing.JButton salvarButton;
    private javax.swing.JPanel salvarFecharPainel;
    private javax.swing.JCheckBox segundaCheckBox;
    private javax.swing.JCheckBox sextaCheckBox;
    private javax.swing.JCheckBox tercaCheckBox;
    // End of variables declaration//GEN-END:variables
    private Cursos curso;
    private Professor professor;
    private CursosDAO cursosDAO;

    public void setData(Object objeto) {
        this.professor = (Professor) objeto;
        nomeProfessorField.setText(professor.getNome());
    }
}
